<template>
  <div>

    <!-- 权限点数据展示 -->
    <el-tree
      ref="ref"
      :data="list"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSave">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permisson'
import { getRoleDetail, assignPerm } from '@/api/settings'
import { tranDataToTree } from '@/utils'

export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permIds: [],
      list: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSave() {
      try {
        const permIds = this.$refs.ref.getCheckedKeys()
        await assignPerm({ id: this.id, permIds })
        // console.log('assignPerm', res)
        // 关闭弹窗
        this.$emit('close')
      } catch (err) {
        console.log('assignPerm', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    },
    async loadPermissionList() {
      try {
        const { data: res } = await getPermissionList()
        // console.log('getPermissionList', res)
        const { data: info } = await getRoleDetail(this.id)
        // console.log('info', info)
        // 保存数据
        this.list = tranDataToTree(res.data)
        // this.permIds = info.data.permIds
        this.$refs.ref.setCheckedKeys(info.data.permIds)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>
